package project3;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class Text {

    public static void main(String[] args) {
        String usersData = "10001:张三:男:1990-01-01#10002:张三:女:1989-01-09#" +
                "10003:王五:男:1999-09-09#10004:刘备:男:1899-01-01#" +
                "10005:孙悟空:男:1900-01-01#10006:张三:女:1999-01-01#" +
                "10007:刘备:女:1999-01-01#10008:张三:女:2003-07-01#" +
                "10009:猪八戒:男:1900-01-01#";

        List<User> users = new ArrayList<>();
        String[] spiltUsersData = usersData.split("#");
        for (int i = 0; i < spiltUsersData.length; i++) {
            //10001:张三:男:1990-01-01
            String[] spiltUserData = spiltUsersData[i].split(":");
            String id = spiltUserData[0];
            String name = spiltUserData[1];
            String gender = spiltUserData[2];
            String birthday = spiltUserData[3];

            //id 需要转化为Long ,生日需要转化为LocalDate
            long idLong = Long.parseLong(id);
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate birthdayLD = LocalDate.parse(birthday, dateTimeFormatter);
            User user = new User(idLong, gender, birthdayLD, name);
            users.add(user);
        }
        System.out.println(users);

        Map<String, Integer> map = new HashMap<>();
        for (User user : users) {
            if (map.containsKey(user.getName())){
                map.put(user.getName(),map.get(user.getName())+1);
            }else {
                map.put(user.getName(),1);
            }
        }
/*        int count = 0;
        for (int i = 0; i < users.size(); i++) {
            count = 1;
            if (contain(users.get(i).getName())) {
                for (int j = i + 1; j < users.size(); j++) {
                    if (users.get(i).getName().equals(users.get(j).getName())) {
                        count++;
                    }
                }
                System.out.println(users.get(i).getName() + " " + count);
                map.put(users.get(i).getName(), count);
            }
        }*/
        System.out.println("------------------------------------------------");
        //遍历集合
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
/*        public static List<String> nameList = new ArrayList<>();
        public static boolean contain (String name){
            if (nameList != null) {
                for (int i = 0; i < nameList.size(); i++) {
                    if (nameList.get(i).equals(name)) {
                        return false;
                    }
                }
            }
            if (nameList != null) {
                nameList.add(name);
            }
            return true;
        }*/
}